var connector = require("../components/winLocation.js");
import {
    setHeaderH,
    viewInit
} from '../views/lodinView.js';
var cw = require("../components/winLocation.js");
import {
    errorDialog
} from '../components/errorDialog.js';

var $lodinBtn, $lodinId, $lodinCard, $lodinAgency;

$(document).on("pageinit", "#lodinPage", lodinInit);

// 登入页面加载好,进行初始化
function lodinInit() {
    hidewxhide();
    console.log("ddddd");
    cw.checkBrowser();
    cw.QueryString.Initial();
    initValue();
    setHeaderH();
    viewInit();
    $lodinBtn.on("click", sentLodin);
    $("#open_btn").on("touchstart", function() {
        location.href = "./apply.html?openid=" + openid;
    });
    loginLast();



}

function hidewxhide(argument) {
    setTimeout(function() {
        wx.hideOptionMenu();
    }, 500);
    setTimeout(function() {
        wx.hideOptionMenu();
    }, 1000);
    setTimeout(function() {
        wx.hideOptionMenu();
    }, 1500);
    setTimeout(function() {
        wx.hideOptionMenu();
    }, 2000);
    setTimeout(function() {
        wx.hideOptionMenu();
    }, 3000);
}

// 取登入参数
function getParameters() {
    var parameters = {};
    parameters.lodinId = $("#lodin_id").val();
    parameters.password = $("#lodin_card").val();
    parameters.agency = $("#lodin_agency").val();
    parameters.position = $("header .weui-flex__item.checked").attr("data-position");
    //获取url中的openid
    parameters.openid = cw.QueryString.GetValue("openid");
    return parameters;
}

// 登入请求
function sentLodin() {
    if ($lodinBtn.hasClass("weui-btn_disabled")) {
        return;
    } else {
        var parameters = getParameters();
        //  alert(TOKEN_LOGIN_URL);
        $.post(TOKEN_LOGIN_URL, parameters, callBack, "text");
    }

}

// 回调函数
function callBack(msg) {
    msg = msg.replace(/:+(\d+)/g, ':"$1"');
    msg = JSON.parse(msg);
    haslogin(msg);
    // msg = JSON.parse(msg);
    var tipmsg;
    if (msg.token_login_response != undefined) {
        if (msg.token_login_response.resp_code == "0000") { //如果成功 
            var res_msg = msg.token_login_response;
            var opendid = res_msg.openid;
            var position = res_msg.opt_type;
            var token = res_msg.token;
            var loginName = res_msg.loginName;
            var brhId = res_msg.brhId;
            setSessionStorage(res_msg);
            setLocalStorage(res_msg);
            if (position == "0") //客户经理登陆
            {
                window.location.href = "./managerHome.html?openid=" + opendid + "&token=" + token + "&loginName=" + loginName + "&loginType=" + position + "&brhId=" + brhId;
            } else if (position == "1") //商户登陆
            {
                window.location.href = "./merchantHome.html";

            } else if (position == "2") //营业员登陆
            {
                window.location.href = "./staffHome.html";
            }
        } else {
            tipmsg = "登录出错";
            if (msg.token_login_response && msg.token_login_response.resp_msg) {
                tipmsg = msg.token_login_response.resp_msg;
            }
            errorObject.showErroDialog(tipmsg);
        }
    } else {
        tipmsg = "当前服务请求出错，请重新查询";
        if (msg.resp_msg) {
            tipmsg = msg.resp_msg;
        }
        errorObject.showErroDialog(tipmsg);
    }
}


function callzdBack(msg) {
    msg = msg.replace(/:+(\d+)/g, ':"$1"');
    msg = JSON.parse(msg);
    var tipmsg;
    if (msg.token_login_response != undefined) {
        if (msg.token_login_response.resp_code == "0000") { //如果成功 
            var res_msg = msg.token_login_response;
            var opendid = res_msg.openid;
            var position = res_msg.opt_type;
            var token = res_msg.token;
            var loginName = res_msg.loginName;
            var brhId = res_msg.brhId;
            setSessionStorage(res_msg);
            setLocalStorage(res_msg);
            if (position == "0") //客户经理登陆
            {
                window.location.href = "./managerHome.html?openid=" + opendid + "&token=" + token + "&loginName=" + loginName + "&loginType=" + position + "&brhId=" + brhId;
            } else if (position == "1") //商户登陆
            {
                window.location.href = "./merchantHome.html";

            } else if (position == "2") //营业员登陆
            {
                window.location.href = "./staffHome.html";
            }
        } else {
            tipmsg = "登录出错";
            if (msg.token_login_response && msg.token_login_response.resp_msg) {
                tipmsg = msg.token_login_response.resp_msg;
            }
            //            errorObject.showErroDialog(tipmsg);
        }
    } else {
        tipmsg = "当前服务请求出错，请重新查询";
        if (msg.resp_msg) {
            tipmsg = msg.resp_msg;
        }
        //        errorObject.showErroDialog(tipmsg);
    }
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录或已登出,请重新登入");
        setTimeout(function() {
            location.href = "./lodin.html" + location.search;
        }, 3000);
    }
}

// 判断登录上次登录的账户
function loginLast() {
    var MerchantNo, //上次登录的机构号
        staffPhone, //上次登录的手机号
        managerName, // 上次登录的客户经理名
        managerNo, // 上次登录的客户经理号
        opt_type; // 上次登录的类型
    opt_type = localStorage.getItem("opt_type");
    var logoutVal = localStorage.getItem("logoutVal");
    MerchantNo = localStorage.getItem("merchantNo");
    staffPhone = localStorage.getItem("staffPhone");
    managerName = localStorage.getItem("managerName");
    managerNo = localStorage.getItem("managerNo");


    if (logoutVal == "2") {
        var oneLogingParamters = {
            "position": opt_type,
            "openid": openid
        }
        if (opt_type == "1") {
            oneLogingParamters.lodinId = MerchantNo;
        } else if (opt_type == "2") {
            oneLogingParamters.lodinId = staffPhone;
        } else if (opt_type == "0") {
            oneLogingParamters.lodinId = managerName;
            oneLogingParamters.agency = managerNo;
        }

        if (opt_type != undefined && oneLogingParamters.lodinId != undefined) {
            $.post(TltdOneLogin_URL, oneLogingParamters, callzdBack, "text");
        }
    }

}


// 设置本地存储SessionStorage
function setSessionStorage(res_msg) {
    var opendid = res_msg.openid;
    var loginType = res_msg.opt_type;
    var token = res_msg.token;
    var loginName = res_msg.loginName;
    var brhId = res_msg.brhId;

    sessionStorage.setItem("tltd_loginName", loginName);
    sessionStorage.setItem("tltd_token", token);
    sessionStorage.setItem("tltd_loginType", loginType);
    sessionStorage.setItem("tltd_openid", opendid);
    sessionStorage.setItem("tltd_brhId", brhId);
}

// 长期存储登录信息
function setLocalStorage(res_msg) {
    localStorage.setItem("opt_type", res_msg.opt_type);
    localStorage.setItem("logoutVal", "2");
    if (res_msg.opt_type == "1") {
        localStorage.setItem("merchantNo", res_msg.loginName);
    } else if (res_msg.opt_type == "2") {
        localStorage.setItem("staffPhone", res_msg.loginName);
    } else if (res_msg.opt_type == "0") {
        localStorage.setItem("managerName", res_msg.loginName);
        localStorage.setItem("managerNo", res_msg.brhId);
    }

}

// 初始化变量
function initValue() {
    $lodinBtn = $("#lodin_btn");
    $lodinId = $("#lodin_id");
    $lodinCard = $("#lodin_card");
    $lodinAgency = $("#lodin_agency");
    window.openid = cw.QueryString.GetValue("openid");
    window.errorObject = new errorDialog();
}